package com.example.treehole;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;

import com.example.treehole.base.BaseBindingActivity;
import com.example.treehole.base.BindAdapter;
import com.example.treehole.bean.Post;
import com.example.treehole.databinding.ActivityCollectBinding;
import com.example.treehole.databinding.ItemPostBinding;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;

import java.util.HashMap;
import java.util.List;

public class CollectActivity extends BaseBindingActivity<ActivityCollectBinding> {


    private BindAdapter<ItemPostBinding, Post> postAdapter = new BindAdapter<ItemPostBinding, Post>() {
        @Override
        public ItemPostBinding createHolder(ViewGroup parent) {
            return ItemPostBinding.inflate(getLayoutInflater(), parent, false);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void bind(ItemPostBinding itemPostBinding, Post post, int position) {
            itemPostBinding.getRoot().setOnClickListener(view -> {
                startActivity(PostDetailActivity.class, intent -> {
                    intent.putExtra("post", post);
                });
            });

            itemPostBinding.tvUpTime.setText(post.time);

            itemPostBinding.title.setText(post.title);
            loadImage(post.face, itemPostBinding.ivFace);
            itemPostBinding.tvUpName.setText(post.nickname);
            if (!post.file.isEmpty()) {
                itemPostBinding.ivImage.setVisibility(View.VISIBLE);
                loadImage(post.file, itemPostBinding.ivImage);
            } else {
                itemPostBinding.ivImage.setVisibility(View.GONE);
            }

        }
    };
    @Override
    protected void initListener() {
    }

    @Override
    protected void initData() {
        viewBinder.rvTips.setAdapter(postAdapter);
        refresh();
    }

    private void refresh() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userid", App.user.id);
        showLoading();
        viewBinder.rvTips.setAdapter(postAdapter);
        HttpUtil.getList(Api.COLLECT_GET, params, Post.class, new HttpUtil.OnNetListListener<Post>() {
            @Override
            public void success(List<Post> data, String message) {
               postAdapter.getData().clear();
               postAdapter.getData().addAll(data);
               postAdapter.notifyDataSetChanged();
                display$Toast(message);
            }

            @Override
            public void error(String message) {
                display$Toast(message);
            }
        });


    }
}